package com.wc.算法提高课.D第四章_高级数据结构.并查集.程序自动分析;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/16 1:15
 * @description https://www.acwing.com/problem/content/239/
 */
public class Main {
    /**
     * 思路：等式可以用并查集来存相等的集合，不相等来查询即可，由于数据范围比较大，用离散化来存储。
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    static Map<Integer, Integer> map = new HashMap<>(N);
    static int[][] w = new int[N][3];
    static int[] p = new int[N];
    static int n, m;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            m = sc.nextInt();
            // this is important, do not forget
            map.clear();
            n = 0;
            for (int i = 1; i <= m; i++) {
                int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
                w[i][0] = get(a);
                w[i][1] = get(b);
                w[i][2] = c;
            }
            // init
            for (int i = 1; i <= n; i++) p[i] = i;

            // merge
            for (int i = 1; i <= m; i++) {
                if (w[i][2] == 1) {
                    int a = w[i][0], b = w[i][1];
                    a = find(a);
                    b = find(b);
                    p[a] = b;
                }
            }
            boolean hasConflict = false;
            // check
            for (int i = 1; i <= m; i++) {
                if (w[i][2] == 0) {
                    int a = w[i][0], b = w[i][1];
                    a = find(a);
                    b = find(b);
                    if (a == b) {
                        hasConflict = true;
                        break;
                    }
                }
            }
            if (hasConflict) out.println("NO");
            else out.println("YES");
        }
        out.flush();
    }

    static int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }

    static int get(int x) {
        if (!map.containsKey(x)) map.put(x, ++n);
        return map.get(x);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
